{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串\n",
    "- 转义字符\n",
    "- 格式化\n",
    "- 内建函数（后延）\n",
    "\n",
    "## 转义字符\n",
    "- 用一个特殊的方法表示出一系列不方便写出的内容，比如回车键，换行键，退格键\n",
    "- 借助反斜杠字符，一旦字符串中出现反斜杠，则反斜杠后面一个火几个字符表示已经不是原来的意思了，进行了转义\n",
    "- 在字符串中，一旦出现反斜杠就要加倍小心，可能由转义字符出现\n",
    "- 不同系统对换行操作有不同的表示\n",
    "    - windows： \\n\n",
    "    - Linux: \\r\\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love lixiaojing\n"
     ]
    }
   ],
   "source": [
    "s = \"i love lixiaojing\"\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let's go\n",
      "Let's go\n",
      "c:\\user\n",
      "I love \r\n",
      " wangixoajing\n"
     ]
    }
   ],
   "source": [
    "# 如果想表示出Let‘s go来\n",
    "# 1. 可以使用嵌套引号，即外层使用双引号\n",
    "# 2. 转义字符\n",
    "s = \"Let's go\"\n",
    "print(s)\n",
    "\n",
    "#\\' == '\n",
    "ss = 'Let\\'s go'\n",
    "print(ss)\n",
    "\n",
    "# \\\\ = \\\n",
    "sss = \"c:\\\\user\"\n",
    "print(sss)\n",
    "\n",
    "# 回车换行符\n",
    "s1 = \"I love \\r\\n wangixoajing\"\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串的格式化\n",
    "- 把字符串按照一定格式打印或者填充\n",
    "- 格式化由两种方法\n",
    "    - 利用百分号（%）\n",
    "    - 利用format函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s = \"xxx 您好， 我们图灵学院开课了，免费的Python呀，不学白不学，学了也白学。\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 利用百分号格式化\n",
    "- 在字符串中，利用%表示一个特殊的含义，表示对字符进行格式化\n",
    "- %d: 此处应该放入一个整数\n",
    "- %s: 表示此处应该放入一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love %s\n"
     ]
    }
   ],
   "source": [
    "s = \"I love %s\"\n",
    "# 下面打印，直接把%s作为字符串的一个内容打印出来\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love wangxiaojing\n"
     ]
    }
   ],
   "source": [
    "print(\"I love %s\"%\"wangxiaojing\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love wangxiaojing\n"
     ]
    }
   ],
   "source": [
    "print( s%\"wangxiaojing\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am %d years old\n",
      "I am 18 years old\n"
     ]
    }
   ],
   "source": [
    "s = \"I am %d years old\"\n",
    "# 留意下面两句话的区别和结果\n",
    "print(s)\n",
    "print(s%18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am %s, i am %d years old\n",
      "I am tulingxueyuan, i am 18 years old\n"
     ]
    }
   ],
   "source": [
    "s = \"I am %s, i am %d years old\"\n",
    "print(s)\n",
    "# 注意以下表达的出错原因\n",
    "# 如果字符串中由占位符，则由几个占位符必须用几个实际内容代替，或者一个也不要\n",
    "#print(s%\"tulingxueyuan\")\n",
    "\n",
    "# 如果出现多个占位符，则相应内容需要用括号括起来\n",
    "print(s%(\"tulingxueyuan\", 18))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### format函数格式化字符串\n",
    "- 直接用ｆｏｒｍａｔ函数进行格式化\n",
    "- 推荐使用这种方法\n",
    "- 在使用上，以{}和：代替%号，后面用format带参数完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love LiXiaojing\n",
      "Yes, i am 18 years old, I love Tulingxueyuan and i am 18 years old\n"
     ]
    }
   ],
   "source": [
    "s = \"I love {}\".format(\"LiXiaojing\")\n",
    "print(s)\n",
    "\n",
    "s = \"Yes, i am {1} years old, I love {0} and i am {1} years old\".format(\"Tulingxueyuan\", 18)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## None\n",
    "- 表示什么都没有\n",
    "- 如果函数没有返回值，可以返回 None\n",
    "- 用来占位置\n",
    "- 用来接触变量绑定\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 表达式\n",
    "- 由一个或者几个数字或者变量和运算符组合成的一行代码\n",
    "- 通常会返回一个结果\n",
    "# 运算符\n",
    "- 由一个以上的值经过变化得到新值的过程就叫运算\n",
    "- 用于运算的符号叫运算符\n",
    "- 运算符分类：\n",
    "    - 算数运算符\n",
    "    - 比较或者关系运算符\n",
    "    - 赋值运算符\n",
    "    - 逻辑运算符\n",
    "    - 位运算\n",
    "    - 成员运算\n",
    "    - 身份运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 算数运算符\n",
    "- 进行算数运算的符号\n",
    "- python没有自增自减运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "36\n",
      "2.25\n",
      "1\n",
      "2\n",
      "6561\n",
      "27\n"
     ]
    }
   ],
   "source": [
    "# +,-跟正常算术运算符一模一样\n",
    "a = 9+3-2\n",
    "print(a)\n",
    "# 乘号用星号(*)代替\n",
    "a = 9 * 4\n",
    "print(a)\n",
    "# 除号用斜杠(/)代替\n",
    "# 在Python2.x 和python3.x 中，除号(/)结果可能不一致，此处以3系列为准\n",
    "a = 9/4\n",
    "print(a)\n",
    "\n",
    "\n",
    "# % 取余运算\n",
    "# 两个数字相除应该有商数有余数\n",
    "# % 只会得到余数\n",
    "a = 9 % 4\n",
    "print(a)\n",
    "\n",
    "# // 表示取商运算，也叫地板除\n",
    "a = 9 // 4\n",
    "print(a)\n",
    "\n",
    "# ** 表示幂运算\n",
    "a = 9 ** 4\n",
    "print(a)\n",
    "\n",
    "a = 3 ** 3\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 比较运算符\n",
    "- 对两个变量或者值进行比较的运算符\n",
    "- 比较的结果是布尔值，即True/False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# == , 等于号\n",
    "a = 3 ** 4\n",
    "# 下面语句执行书序是\n",
    "# 1， 计算 a == 80\n",
    "# 2. 把结果放入b中\n",
    "b = a == 80\n",
    "print(b)\n",
    "\n",
    "\n",
    "# != 不等于\n",
    "print( 9 != 8 )\n",
    "\n",
    "# > 大于\n",
    "# < 小于\n",
    "# >= 大于等于\n",
    "# <= 小于等于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 赋值运算符\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "# = , 赋值\n",
    "a = 0\n",
    "c = a = 4\n",
    "\n",
    "# +=, 是缩写，\n",
    "a = 0\n",
    "a += 7 # a = a+ 7 的缩写\n",
    "print(a)\n",
    "\n",
    "\n",
    "# 所有数学运算符都有缩写形式\n",
    "# -=， ×=， /=, //=, %=, **=,都是缩写形式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑运算符\n",
    "- 对布尔值进行计算的符号\n",
    "- and 逻辑与\n",
    "- or 逻辑或\n",
    "- not 逻辑非\n",
    "- python中逻辑运算没有异或运算\n",
    "- 运算规则：\n",
    "    - and看做乘法， or看做加法，\n",
    "    - True看做1， False看做0\n",
    "    - 则逻辑运算就能转换成整数数学运算\n",
    "    - 最后结果如果是0则为False， 否则为True\n",
    "- 逻辑运算的短路问题\n",
    "    - 逻辑运算式，按照运算顺序计算，一旦能够确定整个式子未来的值，则不再进行计算，直接返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算符案例\n",
    "a = True\n",
    "b = False\n",
    "c = True\n",
    "\n",
    "# 以下式子等价于 d = 1 * 0 + 1\n",
    "d = a and b or c\n",
    "print(d)\n",
    "\n",
    "\n",
    "d = a or b and a\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-43-bb4253784911>, line 9)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-43-bb4253784911>\"\u001b[0;36m, line \u001b[0;32m9\u001b[0m\n\u001b[0;31m    a = a or (b=9) and 6\u001b[0m\n\u001b[0m               ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算的短路案例\n",
    "\n",
    "# 下面的逻辑表达式，a的值一定是True，则运行到or的时候，整个表达式不在向下计算\n",
    "a = True or xxxxxxxxxxx\n",
    "\n",
    "# 下面表达式，如果xxx中包含赋值表达式，则结果很难预期\n",
    "# 代码示例（伪代码)\n",
    "b = 0\n",
    "a = a or (b=9) and 6\n",
    "# 假定上面表达式如果没有语法错误\n",
    "# 则b最终的值应该是0而不是9\n",
    "print(b)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 成员运算符号\n",
    "- 用来检测某一个变量是否是另一个变量的成员\n",
    "- in\n",
    "- not in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 案例\n",
    "l = [1,2,3,4,5]\n",
    "a = 7\n",
    "\n",
    "b = a in l\n",
    "print(b)\n",
    "\n",
    "a = 4\n",
    "print(a in l)\n",
    "\n",
    "print(a not in l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 身份运算\n",
    "- is: 用来检测两个变量是否是同一个变量\n",
    "    - 语法就是  var1 is var2 \n",
    "- is not: 两个变量不是同一个变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = 9\n",
    "b = 9\n",
    "print( a is b)\n",
    "\n",
    "a = \"I love wangxiaojing\"\n",
    "b = \"I love wangxiaojing\"\n",
    "print( a is b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算符的优先级问题\n",
    "- 永远记住，括号具有最高优先级\n",
    "- 优先级表格\n",
    "\n",
    "        **  指数 (最高优先级)\n",
    "        ~ + -   按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)\n",
    "        * / % //    乘，除，取模和取整除\n",
    "        + - 加法减法\n",
    "        >> <<   右移，左移运算符\n",
    "        &   位 'AND'\n",
    "        ^ | 位运算符\n",
    "        <= < > >=   比较运算符\n",
    "        <> == !=    等于运算符\n",
    "        = %= /= //= -= += *= **=    赋值运算符\n",
    "        is is not   身份运算符\n",
    "        in not in   成员运算符\n",
    "        not or and  逻辑运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 程序结构\n",
    "- 顺序\n",
    "- 分支\n",
    "- 循环"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
